From c398fd6db61bc084679759096bde5747e5616eca Mon Sep 17 00:00:00 2001
From: Jann Horn <jann@thejh.net>
Date: Sun, 18 Apr 2021 16:37:16 +0200
Subject: [PATCH] lxsession-logout: Avoid costly lsb_release invocation

Having the name of the distribution in the LXDE prompt looks kinda nice,
but `lsb_release -r -s` is fairly costly (or at least the version on
Debian is): It spins up a python interpreter, then runs `apt-cache policy`
(which in turn, among other things, runs
`/usr/bin/dpkg --print-foreign-architectures` twice).

On a desktop machine with a proper Haswell CPU, `lsb_release -r -s` takes
"only" around 105ms (around 79ms of that are for `apt-cache policy`);
but on an old Intel NUC with a 5-years-old mobile Intel Pentium with a TDP
of 6W, it takes roughly between 500ms and 1000ms.

As suggested by ZanderBrown, use glib's g_get_os_info() instead if
available. If that doesn't exist, it's probably better to omit the OS name
instead of potentially spending up to a second on figuring out what it is.
---
 lxsession-logout/lxsession-logout.c | 33 ++++++-----------------------
 1 file changed, 6 insertions(+), 27 deletions(-)

diff --git a/lxsession-logout/lxsession-logout.c b/lxsession-logout/lxsession-logout.c
index 489e6e3..f06e61d 100644
--- a/lxsession-logout/lxsession-logout.c
+++ b/lxsession-logout/lxsession-logout.c
@@ -716,35 +716,14 @@ int main(int argc, char * argv[])
         if (session_name == NULL)
             session_name = "LXDE";
 
-        gchar *output = NULL;
-
-        if (g_find_program_in_path("lsb_release"))
-        {
-            const gchar *command_line = "lsb_release -r -s";
-            GError *error;
-            if (!g_spawn_command_line_sync( command_line,
-                                            &output,
-                                            NULL,
-                                            NULL,
-                                            &error))
-            {
-
-                fprintf (stderr, "Error: %s\n", error->message);
-                g_error_free (error);
-
-            }
-        }
+        gchar *os_name = NULL;
+#if GLIB_CHECK_VERSION(2, 64, 0)
+        os_name = g_get_os_info(G_OS_INFO_KEY_PRETTY_NAME);
+#endif
 
-        if (output == NULL)
-        {
-            output = "";
-        }
-        else
-        {
-            output[strlen ( output ) - 1] = '\0';
-        }
+        prompt = g_strdup_printf(_("<b><big>Logout %s %s session ?</big></b>"), session_name, os_name ? os_name : "");
 
-        prompt = g_strdup_printf(_("<b><big>Logout %s %s session ?</big></b>"), session_name, output);
+        g_free(os_name);
     }
     gtk_label_set_markup(GTK_LABEL(label), prompt);
     gtk_box_pack_start(GTK_BOX(controls), label, FALSE, FALSE, 4);
